package com.liuchong.rent.controller;

import com.liuchong.rent.entity.Item;
import com.liuchong.rent.entity.PriceList;
import com.liuchong.rent.entity.Rent;
import com.liuchong.rent.service.ItemService;
import com.liuchong.rent.service.PriceListService;
import com.liuchong.rent.service.RentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class RentController {
    @Autowired
    private ItemService itemService;
    @Autowired
    private RentService rentService;
    @Autowired
    private PriceListService priceListService;

    @RequestMapping(value = "calcRent/{id}", method = RequestMethod.GET)
    public Double calcRent(@PathVariable("id") String card_id){
        Double amount = 0.0;
        List<String> damageOrLossList = new ArrayList<>();
        List<String> noDamageOrLossList = new ArrayList<>();
        List<Character> itemTypeList = new ArrayList<>();
        List<Rent> noDamageOrLossRentList = new ArrayList<>();
        List<Rent> rentList = rentService.getRentInfo(card_id);
        for (Rent rent1: rentList) {
            if (rent1.getItem_status() == '0'){
                damageOrLossList.add(rent1.getItem_id());
            }
            else if (rent1.getItem_status() != '0'){
                noDamageOrLossList.add(rent1.getItem_id());
                noDamageOrLossRentList.add(rent1);
            }
        }
        if (damageOrLossList.size() != 0){
            List<Item> itemList = itemService.getLostCostByItemIds(damageOrLossList);
            for (Item item: itemList) {
                amount = amount + item.getLost_cost();
            }
        }
        if (noDamageOrLossList.size() != 0) {
            for (String s: noDamageOrLossList) {
                itemTypeList.add(s.charAt(0));
            }
            List<PriceList> priceListList = priceListService.getPriceListByItemTypes(itemTypeList);
            for (Rent rent: noDamageOrLossRentList) {
                for (PriceList priceList: priceListList) {
                    if (rent.getItem_id().charAt(0) == priceList.getItem_type()) {
                        Long actualDays = (rent.getReturn_Date().getTime() - rent.getBorrowing_date().getTime()) / (24 * 60 * 60 * 1000);
                        Integer lease_term = priceList.getLease_term();
                        Double rentCost = priceList.getRent_cost();
                        if (actualDays <= lease_term) {
                            amount = amount + rentCost;
                        }
                        else if (actualDays > lease_term && actualDays <= lease_term * 2) {
                            amount = amount + rentCost * 3;
                        }
                        else if (actualDays > lease_term * 2 && actualDays <= lease_term * 5L) {
                            amount = amount + rentCost * 10;
                        }
                        else if (actualDays > lease_term * 5L) {
                            amount = amount + rentCost * 10 + Math.ceil(1.0*(actualDays - lease_term * 5L) / lease_term) * rentCost * 3L;
                        }
                    }
                }
            }
        }

        return amount;
    }
}
